namespace = cr_weather

# EMF-style bootstrap. Existing on_startup/on_chronicle_start hooks may still call
# cr_weather.0; initialize the current season once, then let the quarterly decision
# own all future season changes. This also migrates saves from the retired controller.
character_event = {
	id = cr_weather.0
	hide_window = yes
	is_triggered_only = yes
	only_playable = yes

	trigger = {
		ai = no
		has_game_rule = { name = cr_weather value = enabled }
		capital_scope = { has_owner = yes }
		NOT = { has_global_flag = cr_weather_initialized }
	}

	immediate = {
		# Retire every flag used by the old persistent controller architecture.
		clr_global_flag = cr_weather_bootstrap_pending
		clr_global_flag = cr_weather_restart_clock
		clr_global_flag = cr_weather_force_rebuild
		clr_global_flag = cr_weather_clock_started
		clr_global_flag = cr_weather_heartbeat
		clr_global_flag = cr_weather_maintenance_pulse
		any_province = {
			limit = { has_province_flag = cr_weather_controller }
			clr_province_flag = cr_weather_controller
		}

		# Chronicle period calculations require the current calendar year.
		export_to_variable = { which = global_cr_weather_year value = year }

		# EMF season ranges: winter Dec-Feb, spring Mar-May,
		# summer Jun-Aug, autumn Sep-Nov.
		if = {
			limit = {
				OR = {
					month = 11
					AND = {
						month = 0
						NOT = { month = 2 }
					}
				}
			}
			clr_global_flag = cr_weather_season_spring
			clr_global_flag = cr_weather_season_summer
			clr_global_flag = cr_weather_season_autumn
			set_global_flag = cr_weather_season_winter
			# December begins a new four-season annual plan.
			if = {
				limit = { month = 11 }
				capital_scope = { cr_clear_territorial_weather_plans_effect = yes }
			}
		}
		else_if = {
			limit = { month = 2 NOT = { month = 5 } }
			clr_global_flag = cr_weather_season_winter
			clr_global_flag = cr_weather_season_summer
			clr_global_flag = cr_weather_season_autumn
			set_global_flag = cr_weather_season_spring
		}
		else_if = {
			limit = { month = 5 NOT = { month = 8 } }
			clr_global_flag = cr_weather_season_winter
			clr_global_flag = cr_weather_season_spring
			clr_global_flag = cr_weather_season_autumn
			set_global_flag = cr_weather_season_summer
		}
		else = {
			clr_global_flag = cr_weather_season_winter
			clr_global_flag = cr_weather_season_spring
			clr_global_flag = cr_weather_season_summer
			set_global_flag = cr_weather_season_autumn
		}

		capital_scope = { cr_update_world_weather_effect = yes }
		set_global_flag = cr_weather_initialized

		# If startup happened inside an actual quarterly pulse month, lock that
		# quarter so the AI decision cannot immediately repeat the same world pass.
		if = {
			limit = {
				OR = {
					month = 11
					AND = { month = 2 NOT = { month = 3 } }
					AND = { month = 5 NOT = { month = 6 } }
					AND = { month = 8 NOT = { month = 9 } }
				}
			}
			clr_global_flag = cr_weather_quarter_pulse
			set_global_flag = cr_weather_quarter_pulse
		}
	}
}

# Compatibility sink for delayed calls left in saves by the retired controller.
character_event = {
	id = cr_weather.1
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		clr_global_flag = cr_weather_bootstrap_pending
		clr_global_flag = cr_weather_restart_clock
		clr_global_flag = cr_weather_force_rebuild
		clr_global_flag = cr_weather_clock_started
		clr_global_flag = cr_weather_heartbeat
	}
}

# Compatibility sink for queued controller province events in old saves.
province_event = {
	id = cr_weather.2
	hide_window = yes
	is_triggered_only = yes
	immediate = { clr_province_flag = cr_weather_controller }
}

# Compatibility sink for an existing on_monthly_pulse hook. Quarterly scheduling
# is now owned entirely by cr_weather_maintenance in the decisions file.
character_event = {
	id = cr_weather.30
	hide_window = yes
	is_triggered_only = yes
}

# Compatibility sink for calls queued by the retired per-province annual system.
province_event = {
	id = cr_weather.10
	hide_window = yes
	is_triggered_only = yes
	immediate = { clr_province_flag = cr_weather_routed }
}

# One capital-weather entry per player and season, including the initial pass.
character_event = {
	id = cr_weather.20
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_game_rule = { name = cr_weather value = enabled }
		ai = no
		is_ruler = yes
		capital_scope = { has_owner = yes }
		check_variable = { which = global_cr_weather_chronicle_period value = 1 }
		NOT = { check_variable = { which = cr_weather_chronicle_period which = global_cr_weather_chronicle_period } }
	}

	immediate = {
		if = {
			limit = {
				ai = no
				is_ruler = yes
				capital_scope = { has_owner = yes }
				check_variable = { which = global_cr_weather_chronicle_period value = 1 }
				NOT = { check_variable = { which = cr_weather_chronicle_period which = global_cr_weather_chronicle_period } }
			}
			if = {
				limit = { has_global_flag = cr_weather_season_winter }
				chronicle = { entry = CR_CHRONICLE_WEATHER_WINTER }
			}
			else_if = {
				limit = { has_global_flag = cr_weather_season_spring }
				chronicle = { entry = CR_CHRONICLE_WEATHER_SPRING }
			}
			else_if = {
				limit = { has_global_flag = cr_weather_season_summer }
				chronicle = { entry = CR_CHRONICLE_WEATHER_SUMMER }
			}
			else_if = {
				limit = { has_global_flag = cr_weather_season_autumn }
				chronicle = { entry = CR_CHRONICLE_WEATHER_AUTUMN }
			}
			set_variable = { which = cr_weather_chronicle_period which = global_cr_weather_chronicle_period }
		}
	}
}

# The chronicle follows the player on succession, so its weather marker must too.
character_event = {
	id = cr_weather.21
	hide_window = yes
	is_triggered_only = yes
	trigger = { has_game_rule = { name = cr_weather value = enabled } }

	immediate = {
		if = {
			limit = {
				NOT = { check_variable = { which = cr_weather_chronicle_period which = cr_weather_chronicle_period who = FROM } }
			}
			set_variable = { which = cr_weather_chronicle_period which = cr_weather_chronicle_period who = FROM }
		}
	}
}

# Refresh a ruler's character-sheet marker after title or periodic state changes.
character_event = {
	id = cr_weather.22
	hide_window = yes
	is_triggered_only = yes
	only_rulers = yes

	trigger = {
		has_game_rule = { name = cr_weather value = enabled }
		is_ruler = yes
		capital_scope = { has_owner = yes }
	}

	immediate = { cr_refresh_character_weather_effect = yes }
}

# Remove stale character-sheet weather when a character becomes unlanded.
character_event = {
	id = cr_weather.23
	hide_window = yes
	is_triggered_only = yes

	immediate = { cr_clear_character_weather_effect = yes }
}
